//-----------------------------------------------------------------------
// <copyright file="G2Response.cs" company="Robert MacLean">
//     This file is released under the LGPL. For more information see 
//     http://gallery2.codeplex.com.
//     Originally created by Robert MacLean http://www.sadev.co.za
// </copyright>
//-----------------------------------------------------------------------

namespace SADev.Gallery2.Protocol
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Diagnostics;
    using System.Globalization;

    /// <summary>
    /// Represents a parsed G2 response.
    /// </summary>
    public class G2Response
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="G2Response"/> class.
        /// </summary>
        /// <param name="result">The result.</param>
        public G2Response(Collection<string> result)
        {
            result.RemoveAt(0); // remove header
            Dictionary<string, string> values = new Dictionary<string, string>();
            foreach (string line in result)
            {
                Debug.WriteLine(line);
                string[] valueSet = line.Split('=');
                values.Add(valueSet[0], valueSet[1]);
            }

            this.Status = (Status)values.GetIntValue("status");
            this.StatusText = values.GetStringValue("status_text");
            this.DebugUser = values.GetStringValue("debug_user");
            this.DebugTime = values.GetStringValue("debug_time");
            this.ServerVersion = values.GetStringValue("server_version");
            this.DebugCoreVersion = values.GetStringValue("debug_core_version");
            this.RemoteModuleVersion = values.GetStringValue("debug_module_version");
            this.AuthToken = values.GetStringValue("auth_token");

            if (values.ContainsKey("album_count"))
            {
                this.Albums = new Collection<Album>();
                int albumCount = values.GetIntValue("album_count");
                for (int counter = 1; counter < albumCount + 1; counter++)
                {
                    this.Albums.Add(new Album()
                    {
                        Id = counter,
                        Name = values.GetStringValue(String.Format(CultureInfo.CurrentCulture, "album.name.{0}", counter)),
                        Parent = values.GetIntValue(String.Format(CultureInfo.CurrentCulture, "album.parent.{0}", counter)),
                        Summary = values.GetStringValue(String.Format(CultureInfo.CurrentCulture, "album.summary.{0}", counter)),
                        Title = values.GetStringValue(String.Format(CultureInfo.CurrentCulture, "album.title.{0}", counter)),
                        ResizeSize = values.GetStringValue(String.Format(CultureInfo.CurrentCulture, "album.resize_size.{0}", counter)),
                        ThumbnailSize = values.GetStringValue(String.Format(CultureInfo.CurrentCulture, "album.thumb_size.{0}", counter)),
                        MaxSize = values.GetStringValue(String.Format(CultureInfo.CurrentCulture, "album.max_size.{0}", counter)),
                        CanAdd = values.GetStringValue(String.Format(CultureInfo.CurrentCulture, "album.perms.add.{0}", counter)).ToUpperInvariant() == "TRUE",
                        CanWrite = values.GetStringValue(String.Format(CultureInfo.CurrentCulture, "album.perms.write.{0}", counter)).ToUpperInvariant() == "TRUE",
                        CanDeleteItem = values.GetStringValue(String.Format(CultureInfo.CurrentCulture, "album.perms.del_item.{0}", counter)).ToUpperInvariant() == "TRUE",
                        CanDeleteAlbum = values.GetStringValue(String.Format(CultureInfo.CurrentCulture, "album.perms.del_alb.{0}", counter)).ToUpperInvariant() == "TRUE",
                        CanCreateSubAlbums = values.GetStringValue(String.Format(CultureInfo.CurrentCulture, "album.perms.create_sub.{0}", counter)).ToUpperInvariant() == "TRUE",
                        ExtraFields = new Collection<string>(values.GetStringValue(String.Format(CultureInfo.CurrentCulture, "album.extrafields.{0}", counter)).Split(','))
                    });
                }
            }

            this.CanCreateRoot = values.GetStringValue("can_create_root") == "yes";
            if (values.ContainsKey("image_count"))
            {
                this.Images = new Collection<Image>();
                int imageCount = values.GetIntValue("image_count");
                for (int counter = 1; counter < imageCount + 1; counter++)
                {
                    Collection<ImageDimensions> resizedVersionImages = new Collection<ImageDimensions>();
                    if (values.ContainsKey(String.Format(CultureInfo.CurrentCulture, "image.resizedNum.{0}", counter)))
                    {
                        int resizedVersionCount = values.GetIntValue(String.Format(CultureInfo.CurrentCulture, "image.resizedNum.{0}", counter));
                        for (int versionCounter = 1; counter < resizedVersionCount + 1; counter++)
                        {
                            resizedVersionImages.Add(new ImageDimensions()
                            {
                                FileName = values.GetStringValue(String.Format(CultureInfo.CurrentCulture, "image.resized.{0}.name.{1}", versionCounter, counter)),
                                Width = values.GetIntValue(String.Format(CultureInfo.CurrentCulture, "image.resized.{0}.width.{1}", versionCounter, counter)),
                                Height = values.GetIntValue(String.Format(CultureInfo.CurrentCulture, "image.resized.{0}.height.{1}", versionCounter, counter)),
                            });
                        }
                    }

                    this.Images.Add(new Image()
                    {
                        Id = counter,
                        Caption = values.GetStringValue(String.Format(CultureInfo.CurrentCulture, "image.caption.{0}", counter)),
                        Hidden = values.GetStringValue(String.Format(CultureInfo.CurrentCulture, "image.hidden.{0}", counter)) == "yes",
                        RawDimensions = new ImageDimensions()
                        {
                            Width = values.GetIntValue(String.Format(CultureInfo.CurrentCulture, "image.raw_width.{0}", counter)),
                            Height = values.GetIntValue(String.Format(CultureInfo.CurrentCulture, "image.raw_height.{0}", counter)),
                            FileSize = values.GetStringValue(String.Format(CultureInfo.CurrentCulture, "image.raw_filesize.{0}", counter)),
                            FileName = values.GetStringValue(String.Format(CultureInfo.CurrentCulture, "image.name.{0}", counter))
                        },

                        ResizedDimensions = new ImageDimensions()
                        {
                            Width = values.GetIntValue(String.Format(CultureInfo.CurrentCulture, "image.resized_width.{0}", counter)),
                            Height = values.GetIntValue(String.Format(CultureInfo.CurrentCulture, "image.resized_height.{0}", counter)),
                            FileName = values.GetStringValue(String.Format(CultureInfo.CurrentCulture, "image.resizedName.{0}", counter))
                        },

                        ThumbnailDimensions = new ImageDimensions()
                        {
                            Width = values.GetIntValue(String.Format(CultureInfo.CurrentCulture, "image.thumb_width.{0}", counter)),
                            Height = values.GetIntValue(String.Format(CultureInfo.CurrentCulture, "image.thumb_height.{0}", counter)),
                            FileName = values.GetStringValue(String.Format(CultureInfo.CurrentCulture, "image.thumbName.{0}", counter))
                        },

                        Title = values.GetStringValue(String.Format(CultureInfo.CurrentCulture, "image.title.{0}", counter)),
                        Extension = values.GetStringValue(String.Format(CultureInfo.CurrentCulture, "image.forceExtension.{0}", counter)),
                        Clicks = values.GetIntValue(String.Format(CultureInfo.CurrentCulture, "image.clicks.{0}", counter)),
                        ResizedVersion = resizedVersionImages
                    });

                }

                this.AutoResize = values.GetStringValue("auto_resize");
                this.MaxSize = values.GetStringValue("max_size");
                this.AddToBeginning = values.GetStringValue("add_to_beginning") == "true";
                this.ExtraFields = new Collection<string>(values.GetStringValue("extrafields").Split(','));
                this.ExtraFields = new Collection<string>(values.GetStringValue("album.extrafields").Split(','));
                this.AlbumName = values.GetStringValue("album_name");
                this.AlbumName = values.GetStringValue("album.name");
                this.ItemName = values.GetStringValue("item_name");

                if (values.ContainsKey("image.name"))
                {
                    this.Image = new Image();
                    this.Image.RawDimensions = new ImageDimensions()
                    {
                        FileName = values.GetStringValue("image.name"),
                        Width = values.GetIntValue("image.raw_width"),
                        Height = values.GetIntValue("image.raw_height"),
                        FileSize = values.GetStringValue("image.raw_filesize")
                    };

                    this.Image.ResizedDimensions = new ImageDimensions()
                    {
                        Width = values.GetIntValue("image.resized_width"),
                        Height = values.GetIntValue("image.resized_height"),
                        FileName = values.GetStringValue("image.resizedName")
                    };

                    this.Image.ThumbnailDimensions = new ImageDimensions()
                    {
                        Width = values.GetIntValue("image.thumb_width"),
                        Height = values.GetIntValue("image.thumb_height"),
                        FileName = values.GetStringValue("image.thumbName")
                    };

                    this.Image.Caption = values.GetStringValue("image.caption");
                    this.Image.Title = values.GetStringValue("image.title");
                    this.Image.Extension = values.GetStringValue("image.forceExtension");
                    this.Image.Hidden = values.GetStringValue("image.hidden") == "yes";
                }
            }
        }

        /// <summary>
        /// Gets the status.
        /// </summary>
        /// <value>The status.</value>
        protected internal Status Status { get; private set; }

        /// <summary>
        /// Gets the status text.
        /// </summary>
        /// <value>The status text.</value>
        protected internal string StatusText { get; private set; }

        /// <summary>
        /// Gets the debug user.
        /// </summary>
        /// <value>The debug user.</value>
        protected internal string DebugUser { get; private set; }

        /// <summary>
        /// Gets the debug time.
        /// </summary>
        /// <value>The debug time.</value>
        protected internal string DebugTime { get; private set; }

        /// <summary>
        /// Gets the server version.
        /// </summary>
        /// <value>The server version.</value>
        protected internal string ServerVersion { get; private set; }

        /// <summary>
        /// Gets the auth token.
        /// </summary>
        /// <value>The auth token.</value>
        protected internal string AuthToken { get; private set; }

        /// <summary>
        /// Gets the debug core version.
        /// </summary>
        /// <value>The debug core version.</value>
        protected internal string DebugCoreVersion { get; private set; }

        /// <summary>
        /// Gets the remote module version.
        /// </summary>
        /// <value>The remote module version.</value>
        protected internal string RemoteModuleVersion { get; private set; }

        /// <summary>
        /// Gets the albums.
        /// </summary>
        /// <value>The albums.</value>
        protected internal Collection<Album> Albums { get; private set; }

        /// <summary>
        /// Gets the images.
        /// </summary>
        /// <value>The images.</value>
        protected internal Collection<Image> Images { get; private set; }

        /// <summary>
        /// Gets a value indicating whether this instance can create root.
        /// </summary>
        /// <value>
        /// <c>true</c> if this instance can create root; otherwise, <c>false</c>.
        /// </value>
        protected internal bool CanCreateRoot { get; private set; }

        /// <summary>
        /// Gets or sets the auto resize.
        /// </summary>
        /// <value>The auto resize.</value>
        protected internal string AutoResize { get; private set; }

        /// <summary>
        /// Gets or sets the size of the max.
        /// </summary>
        /// <value>The size of the max.</value>
        protected internal string MaxSize { get; private set; }

        /// <summary>
        /// Gets or sets a value indicating whether [add to beginning].
        /// </summary>
        /// <value><c>true</c> if [add to beginning]; otherwise, <c>false</c>.</value>
        protected internal bool AddToBeginning { get; private set; }

        /// <summary>
        /// Gets or sets the extra fields.
        /// </summary>
        /// <value>The extra fields.</value>
        protected internal Collection<string> ExtraFields { get; private set; }

        /// <summary>
        /// Gets or sets the name of the album.
        /// </summary>
        /// <value>The name of the album.</value>
        protected internal string AlbumName { get; private set; }

        /// <summary>
        /// Gets or sets the image.
        /// </summary>
        /// <value>The image.</value>
        protected internal Image Image { get; private set; }

        /// <summary>
        /// Gets or sets the name of the item.
        /// </summary>
        /// <value>The name of the item.</value>
        protected internal string ItemName { get; private set; }
    }
}